jQuery(function ($) {
	var modal = {
		init: function () {
			jQuery(document).on('click', 'a.modal_open.conta', function (e) {
				e.preventDefault();

				jQuery.get("/projeto-fs-grupo-aehv/usuarios/conta", function(data){
					jQuery($.trim(data)).modal({
						closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
						position: ["15%",],
						overlayId: 'modal-overlay',
						containerId: 'modal-container',
						onOpen: modal.open,
						onShow: modal.show,
						onClose: modal.close
					});
				});
			});
		},
		open: function (dialog) {
			var h = 250;

			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						jQuery('#modal-container .modal-content').animate({
							height: h
						}, function () {
							jQuery('#modal-container form').fadeIn(200);
						});
					});
				});
			});
		},
		show: function (dialog) {
			jQuery(document).on('click', '#modal-container .modal-send', function (e) {
				e.preventDefault();

				jQuery('#modal-container .modal-title').html('Salvando...');
				jQuery('#modal-container form').fadeOut(200);

				jQuery('#modal-container .modal-content').animate({
					height: '80px'
				}, function () {
					jQuery('#modal-container .modal-loading').fadeIn(200, function () {
						$.ajax({
							url: '/projeto-fs-grupo-aehv/usuarios/conta',
							data: jQuery('#modal-container form').serialize(),
							type: 'post',
							cache: false,
							dataType: 'html',
							success: function (data) {
								jQuery('#modal-container .modal-loading').fadeOut(200, function () {
									var dataModal = jQuery($.trim(data));
									var containerModal = jQuery('#modal-container');

									if (data && data == "true") {
										jQuery('#modal-container .modal-title').html('O usuário foi salvo.');
										jQuery('#modal-container .modal-content').html('');
										containerModal.attr("style",
										"position: fixed; z-index: 1002; height: 60px; width: 300px; left: 474.5px; top: 15%;");
									} else {
										jQuery('.simplemodal-wrap').html(dataModal.html());
										jQuery('#usuario-conta').removeAttr('style');
										jQuery('#UsuarioContaForm').removeAttr('style');
										containerModal.attr("style",
										"position: fixed; z-index: 1002; height: 500px; width: 400px; left: 474.5px; top: 15%;");
									}
								});
							},
							error: modal.error
						});
					});
				});
			});
		},

		close: function (dialog) {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
						location.reload();
					});
				});
			});
		},
		error: function (xhr) {
			alert(xhr.statusText);
		}
	};

	modal.init();
});